package com.aimee.logtestdemo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    private PrintLogThread printLogThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_start_log_print, R.id.btn_stop_log_print})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_start_log_print:
                startLogPrintThread();
                break;
            case R.id.btn_stop_log_print:
                stopLogPrintThread();
                break;
        }
    }

    private void startLogPrintThread() {
        if (null == printLogThread) {
            printLogThread = new PrintLogThread();
            printLogThread.start();
        } else if (!printLogThread.isAlive()) {
            printLogThread.exit();
            printLogThread = new PrintLogThread();
            printLogThread.start();
        }
    }

    private void stopLogPrintThread() {
        if (null != printLogThread) {
            printLogThread.exit();
        }
    }

    private static class PrintLogThread extends Thread {

        private boolean isStopPrintLogThread = false;
        private int printCount = 0;

        @Override
        public void run() {
            isStopPrintLogThread = false;
            printCount = 0;
            while (!isStopPrintLogThread) {
                printCount++;
                Log.i(MainActivity.class.getName(), "还在循环打印中...  " + printCount);
            }
            Log.i(MainActivity.class.getName(), "退出循环打印了");
        }

        public void exit() {
            isStopPrintLogThread = true;
        }

    }

}
